package com.zha.model;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.*;

/**
 * @Title: 只有光头才能变强
 * @Author: 炸炸
 * @Date: 2024/12/8 16:44
 * 统一封装项目的认证安全用户对象(该对象是Security安全框架能够认识的用户对象)
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class SecurityUser implements UserDetails {
    // 商城后台管理系统用户体系相关用户对象属性
    private Long userId;
    private String username;
    private String password;
    private Integer status;
    private Long shopId;
    private String loginType;
    private Set<String> perms = new HashSet<>();
    // 商城用户购物系统体系相关用户对象属性

    public void setPerms(Set<String> perms) {
        // 创建一个最终处理完成的权限集合
        Set<String> finalPermsSet = new HashSet<>();
        // 循环遍历权限集合
        perms.forEach(perm ->{
            // 判断当前权限是否包含多个,即是否包含,号
            if (perms.contains(",")){
                // 说明有多个权限
                // 通过,号进行分隔
                String[] realPerms = perm.split(",");
                for (String realPerm : realPerms) {
                    finalPermsSet.add(realPerm);
                }
            } else {
                finalPermsSet.add(perm);
            }
        });
        this.perms = finalPermsSet;
    }
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return Collections.emptyList();
    }


    @Override
    public String getPassword() {
        return this.password;
    }

    @Override
    public String getUsername() {
        return this.loginType+this.userId;
    }

    @Override
    public boolean isAccountNonExpired() {
        return this.status == 1;
    }

    @Override
    public boolean isAccountNonLocked() {
        return this.status == 1;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return this.status == 1;
    }

    @Override
    public boolean isEnabled() {
        return this.status == 1;
    }

}
